import React, { Component } from 'react'
import { NavBar, Button, Toast } from 'antd-mobile'
import "./Detail.less"
import { Link } from "react-router-dom"
import { reqCartadd, reqGetgoodsinfo } from '../../http/api'
import Item from 'antd-mobile/lib/popover/Item'
import { connect } from "react-redux"
import { actions, getGoods } from "../../store/modules/detail"
import { getInfo } from "../../store/modules/login"
import { bindActionCreators } from 'redux';
class Detail extends Component {
    // constructor() {
    //     super()
    //     console.log(this.props);
    //     // 初始化数据
    //     this.state = {
    //         goodsinfo: {}
    //     }
    // }

    componentDidMount() {
        let { detail: { reqGetGOODAction }, match: { params: { id } } } = this.props
        reqGetGOODAction(id)
        // let { match: { params: { id } } } = this.props;
        // reqGetgoodsinfo({ id: id }).then(res => {
        //     if (res.data.code == 200) {
        //         console.log(res);
        //         this.setState({
        //             goodsinfo: res.data.list ? res.data.list[0] : {}
        //         })
        //     }
        // })



    }
    // add() {
    //     // 1.从本地存储取出uid
    //     let uid = JSON.parse(sessionStorage.getItem("user")).uid
    //     // 2.取id 和type
    //     let { match: { params: { id } } } = this.props
    //     console.log(id);
    //     reqCartadd({ uid, goodsid: id, type: 1, num: 1 }).then(res => {
    //         if (res.data.code === 200) {
    //             Toast.success(res.data.msg, 1)
    //         }
    //     })
    // }
    render() {
        // let {goodsinfo} = this.state
        let { good, match: { params: { id, type } }, info, detail: { goShop } } = this.props
        console.log(this.props);
        return (
            <div className="home">
                <NavBar>详情</NavBar>
                {/* 渲染数据 */}
                <div>
                    <img src={good.img} alt="" />
                    <p>商品名称： {good.goodsname}</p>
                    <p>商品价格：{good.market_price}</p>
                    <Button onClick={() => this.props.history.push("/index/shop")}>购物车</Button>
                    <Button onClick={() => goShop({
                        uid: info.uid,
                        type,
                        goodsid: id,
                        num: 1
                    })}>加入购物车</Button>
                </div>


            </div>
        )
    }
}
// 取数据
const mapStateToProps = (state) => {
    return {
        //1.7 取数据
        // getnewList: getgetnewList(state),
        good: getGoods(state),
        info: getInfo(state)

    }
}
// 取方法的
const mapDispatchToProps = (dispatch) => {
    return {
        //1.8 将方法取回来
        detail: bindActionCreators(actions, dispatch),
    };
};
export default connect(mapStateToProps, mapDispatchToProps)(Detail);
